package com.maxmlr.music.admin.controller;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.beans.factory.annotation.Autowired;
import com.maxmlr.music.admin.service.DeptService;
import com.maxmlr.music.admin.entity.Dept;
import io.swagger.annotations.*;

import java.io.Serializable;
import java.util.Objects;

/**
 * @author max
 * @version 1.0
 * @ClassName AdminDeptController
 * @Description 部门管理 页面跳转控制器
 * @Copyright Copyright (c) 2020: www.maxmlr.com.
 * @since 2020-06-14
 */
@Slf4j
@Controller
@Api(value = "/admin/dept", tags = "Dept 页面跳转控制器")
@RequestMapping("/admin/dept")
public class AdmDeptController {

    @Autowired
    public DeptService service;

    /**
     *  功能列表主页
     *
     * @return string
     */
    @GetMapping
    @RequiresPermissions("admin:dept:dept")
    public String dept() {
        return "admin/dept/dept";
    }

    /**
     *  添加页面
     *
     * @return string
     */
    @GetMapping("/add")
    @RequiresPermissions("admin:dept:add")
    public String add(Integer parentId, Model model){
        Dept dept = service.getById(parentId);
        if (dept == null) {
            dept = new Dept();
            dept.setId(0);
            dept.setName("");
        }
        model.addAttribute("parent", dept);
        return "admin/dept/add";
    }

    /**
     *  编辑页面
     *
     * @return string
     */
    @GetMapping("/edit/{id}")
    @RequiresPermissions("admin:dept:edit")
    public String edit(@PathVariable("id") Serializable id , Model model) {
        Dept dept = service.getById(id);
        model.addAttribute("dept", dept);
        if (dept.getParentId() > 0) {
            Dept parent = service.getById(dept.getParentId());
            if (Objects.nonNull(parent)) {
                model.addAttribute("parentName", parent.getName());
            }
        }
        return "admin/dept/edit";
    }

    @GetMapping("/treeView")
    @RequiresPermissions("admin:dept:dept")
    public String treeView() {
        return "admin/dept/deptTree";
    }

}
